#include <my_header.h>
#include "acceptor.hpp"
#include <iostream>


namespace rocket 
{

acceptor::acceptor(int port,string address)
:_server_fd()
,_network_address(port,address)
{

}    

acceptor::~acceptor()
{
    
}

void acceptor::start()
{
    int one = 1;
    int ret = setsockopt(_server_fd.get_socket(),
                         SOL_SOCKET,
                         SO_REUSEADDR,
                         &one,
                         sizeof(one));
    if(ret < 0) {
        perror("setsockopt");
    }
    ret = setsockopt(_server_fd.get_socket(),
                         SOL_SOCKET,
                         SO_REUSEPORT,
                         &one,
                         sizeof(one));
    if(ret < 0) {
        perror("setsockopt");
    }
    bind();
    listen();
}
int acceptor::accept()
{
    int fd=::accept(_server_fd.get_socket(),nullptr,nullptr);
    return fd;
}
int acceptor::get_fd()
{
    return _server_fd.get_socket();
}

void acceptor::bind()
{
    int ret=::bind(_server_fd.get_socket(),_network_address.get_pointer(),_network_address.get_size());
    if(ret==-1)
    {
        perror("bind");
    }
}

void acceptor::listen()
{
    int ret=::listen(_server_fd.get_socket(),100);
    if(ret==-1)
    {
        perror("listen");
    }
}

}




